package com.af.security.mgt.user;

import com.af.security.mgt.role.Role;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashSet;

/**
 * @author : zhenyun.su
 * @Common :
 *     Long id;
 *     String username; 账号
 *     String password;  密码
 *     String nickName; 用户名称
 *     String email;  邮箱
 *     String phone; 手机号
 *     String employeeID; 员工工号
 *     Boolean enabled;  false 禁用， true 启用
 *     Boolean accountNonExpired;  账号过期
 *     Boolean accountNonLocked;   锁定
 *     Boolean credentialsNonExpired;  密码过期
 *     String sourceId;  唯一的， 值由source
 *     String sourceType;  来源类型， db, ad, ldap, qq, wechat, taobao
 * @since : 2020/09/21
 */

@Entity
@Table(name = "sys_user",  indexes = {@Index(columnList = "user_name", unique = true)})
public class User implements Serializable {
    public static final String SOURCE_TYPE_AD="ldap";
    public static final String SOURCE_TYPE_PHONE="phone";
    @Id
    @Column(name = "user_id")
    private Long id;
    @Column(name = "user_name")
    private String username;
    private String password;
    private String nickName;
    private String email;
    private String phone;
    private String employeeId;
    private Boolean enabled;
    private Boolean accountNonExpired;
    private Boolean accountNonLocked;
    private Boolean credentialsNonExpired;
    private String sourceId;
    private String sourceType;

    private String createdBy;
    private LocalDateTime createdDate;
    private String lastModifiedBy;
    private LocalDateTime lastModifiedDate;


    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_role", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Collection<Role> roles = new HashSet<>();

    public User() {
        super();
        this.enabled = false;
        this.accountNonExpired = true;
        this.accountNonLocked = true;
        this.credentialsNonExpired = true;
        this.createdBy="system";
        this.createdDate=LocalDateTime.now();
        this.lastModifiedBy="system";
        this.lastModifiedDate=LocalDateTime.now();
    }

    public static User of(Long id, String username, String password, String nickName, String email, String phone,  String employeeID, String sourceId, String sourceType){
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setNickName(nickName);
        user.setEmail(email);
        user.setPhone(phone);
        user.setEmployeeID(employeeID);
        user.setSourceId(sourceId);
        user.setSourceType(sourceType);
        user.setAccountNonExpired(true);
        user.setAccountNonLocked(true);
        user.setCredentialsNonExpired(true);

        return user;
    }

    public static User of(Long id, String username, String password, String nickName, String email, String phone,  String employeeID, String sourceId, String sourceType, Boolean enabled){
        User user = of(id,username, password, nickName, email, phone, employeeID, sourceId, sourceType);
        user.setEnabled(enabled);
        return user;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmployeeID() {
        return employeeId;
    }

    public void setEmployeeID(String employeeId) {
        this.employeeId = employeeId;
    }


    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Boolean getAccountNonExpired() {
        return accountNonExpired;
    }

    public void setAccountNonExpired(Boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public Boolean getAccountNonLocked() {
        return accountNonLocked;
    }

    public void setAccountNonLocked(Boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public Boolean getCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public String getSourceId() {
        return sourceId;
    }

    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
    }

    public String getSourceType() {
        return sourceType;
    }

    public void setSourceType(String sourceType) {
        this.sourceType = sourceType;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public LocalDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public Collection<Role> getRoles() {
        return roles;
    }

    public void setRoles(Collection<Role> roles) {
        this.roles = roles;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", employeeID='" + employeeId + '\'' +
                ", enabled=" + enabled +
                ", accountNonExpired=" + accountNonExpired +
                ", accountNonLocked=" + accountNonLocked +
                ", credentialsNonExpired=" + credentialsNonExpired +
                ", sourceId='" + sourceId + '\'' +
                ", sourceType='" + sourceType + '\'' +
                ", createdBy='" + createdBy + '\'' +
                ", createdDate=" + createdDate +
                ", lastModifiedBy='" + lastModifiedBy + '\'' +
                ", lastModifiedDate=" + lastModifiedDate +
                '}';
    }
}
